package cn.com.hatech.blog.module.admin.category.controller;

import cn.com.hatech.blog.common.constant.Types;
import cn.com.hatech.blog.common.controller.BaseController;
import cn.com.hatech.blog.module.admin.category.entity.Meta;
import cn.com.hatech.blog.module.admin.category.service.IMetaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.controller
 * @className: CategoryController
 * @author: xuruikai
 * @description: 分类管理控制器
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@Controller
@RequestMapping("admin/category")
@Slf4j
public class CategoryPageController extends BaseController {

    /**
     * 分类管理业务层接口注入
     */
    @Autowired
    private IMetaService metaService;

    /**
     * 分类/标签 查询
     *
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(HttpServletRequest request) {
        List<Meta> metas = metaService.selectAllMetas();
        // 分类
        List<Meta> categories = metas.stream().filter(meta -> Types.CATEGORY.getType().equals(meta.getType())).collect(Collectors.toList());
        request.setAttribute("categories", categories);
        // 标签
        List<Meta> tags = metas.stream().filter(meta -> Types.TAG.getType().equals(meta.getType())).collect(Collectors.toList());
        request.setAttribute("tags", tags);
        return "admin/category/category";
    }

}
